
#include "CommandLine.h"
#include <Windows.h>

CCommandLine::CCommandLine()
{
    
}

CCommandLine::~CCommandLine()
{
    m_cmd.clear();
}

int CCommandLine::Split(wchar_t chSplit)
{
    if ('\0' == chSplit)
    {
        return -1;
    }
    m_cmd.clear();
    
    int argCount;
    LPWSTR* strArgList = CommandLineToArgvW(GetCommandLine(), &argCount);

    m_path = strArgList[0];

    for (int i = 1; argCount > i; i++)
    {
        __Split(strArgList[i], chSplit);
    }

    m_split = chSplit;

    return m_cmd.size();
}

bool CCommandLine::__Split(const wchar_t* strCommand, const wchar_t chSplit)
{
    int nOffset = 0;
    std::wstring strkey;
    std::wstring strValue;
    
    for (nOffset = 0; strCommand[nOffset] && chSplit != strCommand[nOffset]; nOffset++);

    if (0 == nOffset)
    {
        return false;
    }

    strkey.append(strCommand, nOffset);

    do
    {
        if ('\0' == strCommand[nOffset])
        {
            break;
        }

        if (strCommand[nOffset] == chSplit)
        {
            nOffset++;
        }

        if ('\0' == strCommand[nOffset])
        {
            break;
        }

        strValue.append(&strCommand[nOffset]);
    } while (0);

    m_cmd[strkey] = strValue;

    return true;
}

bool CCommandLine::GetPath(std::wstring& strPath)
{
    strPath = m_path;
    return !!strPath.size();
}

bool CCommandLine::Find(const std::wstring& strKey, std::wstring& strValue)
{
    strValue.clear();
    auto it = m_cmd.find(strKey);
    if (it != m_cmd.end())
    {
        strValue = it->second;
    }

    return !!strValue.size();
}